﻿using System;
using System.Web;

namespace msBsc.AppLogic.Cfg
{
    public class SystemFileDir
    {
        public static SystemFileDir FileDir = new SystemFileDir();
        public SystemFileDir()
        {
            //initUrlBase();
            //inithttpUrlBase();
            //initappPath(); 
            //initstrTemXmlFileDir();
            //initShareMemDir();
        }

        public void initFileDir()
        {
        }

        //public string UrlBase = "";
        /// <summary>
        /// 根目录，以/结尾
        /// </summary>
        public string UrlBase
        {
            get
            {
                if (HttpContext.Current == null)
                {
                    return SystemSet.oSystemSet.dirDumm;
                }
                else
                {
                    string path = HttpContext.Current.Request.ApplicationPath;
                    if (!path.EndsWith("/")) path += "/";


                    return path;
                }
            }
        }

        /// <summary>
        /// 对外发布的网站地址,以/结尾
        /// </summary>
        public string httpUrl
        {
            get
            {
                return SystemSet.oSystemSet.ConfigHttpUrlBase;// ApplicationConst.Const.GetAppSettings("HttpPath");
            }
        }

        /// <summary>
        /// 带http的根目录，以/结尾
        /// </summary>
        public string httpUrlBase
        {
            get
            {
                if (HttpContext.Current == null)
                {
                    return SystemSet.oSystemSet.ConfigHttpUrlBase;// ApplicationConst.Const.GetAppSettings("HttpPath");
                }
                else
                {
                    string path = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath;


                    if (!path.EndsWith("/")) path += "/";

                    return path;
                }
            }
        }

        /// <summary>
        /// 默认国家信息
        /// </summary>
        public string DefaultCountry
        {
            get
            {
                return SystemSet.oSystemSet.DefaultCountry;
            }
        }

        /// <summary>
        /// 默认城市信息
        /// </summary>
        public string DefaultCity
        {
            get
            {
                return SystemSet.oSystemSet.DefaultCity;
            }
        }

        /// <summary>
        /// 默认目的地信息
        /// </summary>
        public string DefaultDestination
        {
            get
            {
                return SystemSet.oSystemSet.DefaultDestination;
            }
        }

        private string _CurrAppPath = "";
        /// <summary>
        /// 程序主目录,WEB站点目录，以\\结尾
        /// </summary>
        public string CurrAppPath
        {
            get
            {
                if (_CurrAppPath == string.Empty)
                {
                    if (HttpContext.Current == null)
                    {
                        _CurrAppPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                    }
                    else
                    {
                        _CurrAppPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath);
                    }
                    if (!_CurrAppPath.EndsWith("\\"))
                    {
                        _CurrAppPath = _CurrAppPath + "\\";
                    }
                }
                return _CurrAppPath;
            }
        }

        /// <summary>
        /// 日志文件目录
        /// </summary>
        public string LogFolder
        {
            get
            {
                if (!string.IsNullOrEmpty(SystemSet.oSystemSet.GetAppSettings("LogFolder")))
                    return SystemSet.oSystemSet.GetAppSettings("LogFolder");
                else 
                    return CurrAppPath + "Log\\";
            }
        }
          

        /// <summary>
        /// /根据物理路径返回虚拟目录路径
        /// </summary>
        /// <param name="strPhypath"></param>
        /// <returns></returns>
        public string GetVFilePath(string strPhypath)
        {
            if (!string.IsNullOrEmpty(strPhypath))
            {
                return strPhypath.Replace(CurrAppPath, "").Replace("\\", "/");
            }
            else
            {
                return "";
            }
        }  

        //public System.Random rd = new Random();
        /// <summary>
        /// 随机数唯一标识文件名
        /// </summary>
        public string RandomFileNme
        {
            get
            {
                byte[] bttem = Guid.NewGuid().ToByteArray();
                long i = 1;
                foreach (byte b in bttem)
                {
                    i *= ((int)b + 1);
                }
                return string.Format("{0:x}", i - DateTime.Now.Ticks);
                //string strFileNme = System.DateTime.Now.ToString("yyMddHHmmss") + rd.Next(1111000, 99999999).ToString();
                //return strFileNme;
            }
        }

        /// <summary>
        /// 返回当前系统的临时文件夹的路径。
        /// </summary>
        public string strSysTempPath
        {
            get
            {
                return System.IO.Path.GetTempPath();
            }
        }
         

        //private DateTime dtlastdel = DateTime.Now.AddMinutes(-33);
        //private string _strTemXmlFileDir = "";
        //private string _strTemXmlFileVDir = "";
        //private void initstrTemXmlFileDir()
        //{
        //    _strTemXmlFileDir = CurrAppPath + "UpLoad\\Cust\\XmlTem\\";
        //    if (!System.IO.Directory.Exists(_strTemXmlFileDir))
        //        System.IO.Directory.CreateDirectory(_strTemXmlFileDir);
        //    _strTemXmlFileVDir = "UpLoad/Cust/XmlTem/";
        //}

        private DateTime dtLastDeltemFile = DateTime.Now.AddMinutes(-10);

        ///// <summary>
        ///// 网站临时文件目录物理路径，将自动删除过期文件,以\结尾
        ///// </summary>
        //public  string strTemXmlFileDir
        //{
        //    get
        //    {
        //        if (dtLastDeltemFile.AddMinutes(10) < DateTime.Now)
        //        {
        //            DelTemXmlFile();
        //            dtLastDeltemFile = DateTime.Now;
        //        }
        //        return _strTemXmlFileDir;
        //    }
        //}

        private void DelTemXmlFile()
        {
            if(!System.IO.Directory.Exists(CurrAppPath + "UpLoad\\Cust\\XmlTem\\"))
            {
                System.IO.Directory.CreateDirectory(CurrAppPath + "UpLoad\\Cust\\XmlTem\\");
                return;
            }
            string[] arrFName = System.IO.Directory.GetFiles(CurrAppPath + "UpLoad\\Cust\\XmlTem\\");
            //int i = 0;
            foreach (string strFName in arrFName)
            {
                //if (i >= 10)
                //    break;
                //i++;
                if (System.IO.File.GetLastWriteTime(strFName).CompareTo(DateTime.Now.AddMinutes(-40)) < 0)
                {
                    //开始一个非事务范围   
                    using (System.Transactions.TransactionScope scope2 = new System.Transactions.TransactionScope(
                        System.Transactions.TransactionScopeOption.Suppress))
                    {
                        try
                        {
                            System.IO.File.Delete(strFName);
                        }
                        catch { }
                    }
                }
            }
        }
        /// <summary>
        /// 网站临时文件夹虚拟目录，以/结尾
        /// </summary>
        public string strTemXmlFileVDir
        {
            get
            {
                DelTemXmlFile();
                return "UpLoad/Cust/XmlTem/";
            }
        }
        //private void initstrTemXmlFileVDir()
        //{
        //    if (!System.IO.Directory.Exists(FileAppPathDir + "UpLoad\\Cust\\XmlTem\\"))
        //        System.IO.Directory.CreateDirectory(FileAppPathDir + "UpLoad\\Cust\\XmlTem\\");
        //    strTemXmlFileVDir = "UpLoad/Cust/XmlTem/";
        //}

        ///// <summary>
        ///// 保存文件的目录,按月份、天建目录
        ///// </summary>
        //public  string strSaveUploadSysFileDir
        //{
        //    get
        //    {
        //        string strTemXmlFileDirtem1 = CurrAppPath + "UpLoad\\System\\" + DateTime.Now.ToString("yyMM") + "\\" + DateTime.Now.ToString("dd") + "\\";
        //        if (!System.IO.Directory.Exists(strTemXmlFileDirtem1))
        //            System.IO.Directory.CreateDirectory(strTemXmlFileDirtem1);
        //        return strTemXmlFileDirtem1;
        //    }
        //}

        public string strTemResFileVDir
        {
            get
            { 
                return "UpLoad/Cust/ResTem/";
            }
        }
        /// <summary>
        /// 保存文件的目录,按月份、天建目录
        /// </summary>
        public string strSaveUploadSysFileDirV
        {
            get
            {
                string strTemXmlFileDirtem1 = "UpLoad/System/" + DateTime.Now.ToString("yyMM") + "/" + DateTime.Now.ToString("dd") + "/";
                if (!System.IO.Directory.Exists(CurrAppPath + strTemXmlFileDirtem1.Replace("/", "\\")))
                {
                    //开始一个非事务范围   
                    using (System.Transactions.TransactionScope scope2 = new System.Transactions.TransactionScope(
                        System.Transactions.TransactionScopeOption.Suppress))
                    {
                        try
                        {
                            System.IO.Directory.CreateDirectory(CurrAppPath + strTemXmlFileDirtem1.Replace("/", "\\"));
                        }
                        catch { }
                    }
                }
                //GetSaveUploadSysFileDir(strTemXmlFileDirtem1);
                return strTemXmlFileDirtem1;
            }
        }
        ///// <summary>
        ///// 传入一个虚拟路径，自动加上根目录后再转换为物理路径返回,可用strSaveUploadSysFileDirV属性获取虚拟目录
        ///// </summary>
        ///// <param name="_strSaveUploadSysFileDirV"></param>
        ///// <returns></returns>
        //public string GetSaveUploadSysFileDir(string _strSaveUploadSysFileDirV)
        //{
        //    try
        //    {
        //        string strTemXmlFileDirtem1 = (CurrAppPath + _strSaveUploadSysFileDirV.Replace("/", "\\")).Replace("\\\\", "\\");
                 
        //        if (!System.IO.Directory.Exists(strTemXmlFileDirtem1))
        //            System.IO.Directory.CreateDirectory(strTemXmlFileDirtem1);
        //        return strTemXmlFileDirtem1;
        //    }
        //    catch
        //    {
        //        return "";
        //    }
        //}

        ///// <summary>
        ///// 获取虚拟路径的物理路径
        ///// </summary>
        ///// <param name="strpVpath"></param>
        ///// <returns></returns>
        //public string GetMapPathByVpath(string strpVpath)
        //{
        //    try
        //    {
        //        string strTemXmlFileDirtem1 = (CurrAppPath + strpVpath.Replace("/", "\\")).Replace("\\\\", "\\");
        //        System.IO.FileInfo oooo = new System.IO.FileInfo(strTemXmlFileDirtem1);
        //        if (!oooo.Directory.Exists)
        //            oooo.Directory.Create();

        //        return strTemXmlFileDirtem1;
        //    }
        //    catch
        //    {
        //        return "";
        //    }
        //}

        ///// <summary>
        ///// 传入一个虚拟文件路径，自动加上根目录后再转换为文件物理路径返回
        ///// </summary>
        ///// <param name="_strSaveUploadSysFileDirV"></param>
        ///// <returns></returns>
        //public  string GetSaveUploadSysFilePath(string _strSaveUploadSysFilePathV)
        //{
        //    //开始一个非事务范围   
        //    using (System.Transactions.TransactionScope scope2 = new System.Transactions.TransactionScope(
        //        System.Transactions.TransactionScopeOption.Suppress))
        //    {
        //        try
        //        {
        //            string strTemXmlFileDirtem1 = (CurrAppPath + _strSaveUploadSysFilePathV.Replace("/", "\\")).Replace("\\\\", "\\");
        //            //string strTemXmlFileDirtem1 = HttpContext.Current.Server.MapPath(UrlBase + _strSaveUploadSysFilePathV);
        //            if (!System.IO.File.Exists(strTemXmlFileDirtem1))
        //            {
        //                return "";
        //            }
        //            else
        //            {
        //                return strTemXmlFileDirtem1;
        //            }
        //        }
        //        catch
        //        {
        //            return "";
        //        }
        //    }
        //}
         

    }
}
